package com.examsystem.service.impl;


import com.examsystem.dao.PaperMapper;
import com.examsystem.dao.UserMapper;
import com.examsystem.dao.UserPaperMapper;
import com.examsystem.pojo.UserPaper;
import com.examsystem.service.UserPaperService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.examsystem.Const.ConstMsg.*;

/**
 * @author fire'ge
 */
@Service("userPaperServiceImpl")
public class UserPaperServiceImpl implements UserPaperService {
    @Resource
    private UserPaperMapper userPaperMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private PaperMapper paperMapper;
    @Override
    public int addUserPaper(UserPaper userPaper) {
        if (userPaper==null){
            throw new RuntimeException(ADD_UP_NULL_MSG);
        }
        boolean flag = userMapper.queryUserMsgByUserId(userPaper.getUpUser())==null
                ||paperMapper.queryPaperById(userPaper.getUpPaper())==null;
        if (flag){
            throw new RuntimeException(CHECK_MSG_FAIL_MSG);
        }
        int addUserPaper = userPaperMapper.addUserPaper(userPaper);
        if (addUserPaper==0){
            throw new RuntimeException(ADD_UP_FAIL_MSG);
        }
        return addUserPaper;
    }


    @Override
    public int updateUserPaper(int userId) {
        if (userMapper.queryUserById(userId)==null){
            throw new RuntimeException(USER_NONE_MSG);
        }
        UserPaper userPaper = userPaperMapper.queryUserPaperById(userId);
        return userPaperMapper.updateUserPaper(userPaper);
    }

}
